// components/indexSearchBar/indexSearchBar.js
import api from '../../utils/api' // 引入api接口管理文件
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    modalType: {
      type: String,
      value: ''
    },
    provincesList: {
      type: Array,
      value: []
    },
    gender: {
      type: Number,
      value: 0
    },
    injectObjectsToNumber: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    cityChoose: 0,
    provinceChoose: 0,
    cityList: [],
    typeList: [{
        name: 'HPV9价疫苗',
        id: 1
      },
      {
        name: 'HPV2价疫苗',
        id: 2
      },
      {
        name: 'HPV3价疫苗',
        id: 3
      }
    ],
    typeId: '',
    // gender: 0,
    injectObjects: 0
  },

  attached: function () {
    this.setData({
      injectObjects: this.properties.injectObjectsToNumber
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    chooseProvince(e) {
      let provinceId = e.currentTarget.dataset.id * 1
      this.setData({
        provinceChoose: provinceId,
        cityList: []
      })
      if (provinceId < 0) {
        this.closeModal({
          cityId: -100,
          cityName: '全国'
        })
        return
      }
      this.getCityList(provinceId)
    },
    getCityList(provinceId) {
      api.areaCities({ // 调用接口，传入参数
        data: {
          provinceId: provinceId
        },
        success: res => {
          if (res.code * 1 === 0) {
            console.log(res)
            this.setData({
              cityList: res.data
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      })
    },
    chooseCity(e) {
      let cityId = e.currentTarget.dataset.id * 1
      let cityName = e.currentTarget.dataset.city
      console.log(e)
      this.setData({
        cityChoose: cityId
      })
      this.closeModal({
        cityId: cityId,
        cityName: cityName
      })
    },
    closeModal(chooseVal) {
      console.log(chooseVal)
      this.triggerEvent("closeModal", chooseVal)
    },
    stop(e) {
      console.log(e)
    },
    chooseGender(e) {
      let gender = e.currentTarget.dataset.type * 1
      this.setData({
        gender: gender
      })
    },
    chooseInjectObjects(e) {
      let injectObjects = e.currentTarget.dataset.type * 1
      console.log(injectObjects)
      this.setData({
        injectObjects: injectObjects
      })
    },
    review() {
      this.setData({
        gender: 0,
        injectObjects: 0
      }, () => {
        this.typeSure()
      })
    },
    typeSure() {
      let injectObjectsArray = []
      switch (this.data.injectObjects * 1) {
        case 0:
          injectObjectsArray = ["儿童", "成人"]
          break
        case 1:
          injectObjectsArray = ["儿童"]
          break
        case 2:
          injectObjectsArray = ["成人"]
          break
      }
      this.closeModal({
        gender: this.data.gender,
        injectObjects: injectObjectsArray,
        injectObjectsToNumber: this.data.injectObjects
      })
    },
    closeMask () {
      console.log('closeMask')
      this.triggerEvent("closeModal")
    }
  }
})